package com.kaesar.leetcode.LeetCode_001_050;

import java.util.*;

public class LeetCode_046 {
    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        int count = 0;
        List<Integer> list = new ArrayList<>();
        Queue<List<Integer>> temp = new LinkedList<>();
        temp.add(list);
        while (count < nums.length) {
            int times = temp.size();
            while (times > 0) {
                List<Integer> cur = temp.poll();
                for (int num : nums) {
                    List<Integer> next = new ArrayList<Integer>(Arrays.asList(new Integer[cur.size()]));
                    Collections.copy(next, cur);
                    if (!next.contains(num)) {
                        next.add(num);
                        temp.add(next);
                    }
                }
                times--;
            }
            count++;
        }
        result.addAll(temp);
        return result;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2};
        for (List<Integer> integers : permute(nums)) {
            for (Integer integer : integers) {
                System.out.print(integer + " ");
            }
            System.out.println();
        }
    }
}
